package modules;

import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.dao.sql.Sql;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;
import org.nutz.resource.Scans;

import dto.CD_Menu;


public class WebSetUp implements Setup{
	
	@Override
	public void init(NutConfig nc) {
		Dao dao = nc.getIoc().get(Dao.class) ;
		for (Class<?> klass : Scans.me().scanPackage("dto"))
			if (klass.getAnnotation(Table.class) != null) {
				dao.create(klass, false);
			}
		/**
		 * 这里判断，如果已经有数据，则不做操作，如果还没有，则插入新数据
		 */
		String path = Mvcs.getServletContext().getRealPath("/") ;
		String filePath = path+"initSqls/menu.csv" ;
		if(dao.count(CD_Menu.class)==0){//说明没有初始化
			String sqlStr = "insert into CD_Menu (menuId,menuName,menuUrl,displayNo,parentId) (select menuId,menuName,menuUrl,displayNo,parentId from CSVREAD('" + filePath + "',NULL,'charset=UTF-8') order by menuId asc)" ;
			Sql sql = Sqls.create(sqlStr) ;
			dao.execute(sql) ;
		}
	}

	@Override
	public void destroy(NutConfig nc) {
		
	}

}
